from docx import Document

doc = Document(r'D:\ai\projects\python\homework\模板.docx')
table = doc.tables[0]

format={}
def copy_paragraph_format(source_paragraph, target_paragraph):
    """
    复制源段落的格式到目标段落
    :param source_paragraph: 源段落对象
    :param target_paragraph: 目标段落对象
    """
    # 复制段落格式
    target_paragraph.style = source_paragraph.style
    target_paragraph.paragraph_format.alignment = source_paragraph.paragraph_format.alignment
    target_paragraph.paragraph_format.line_spacing = source_paragraph.paragraph_format.line_spacing
    target_paragraph.paragraph_format.space_before = source_paragraph.paragraph_format.space_before
    target_paragraph.paragraph_format.space_after = source_paragraph.paragraph_format.space_after

    # 复制段落中的每个 run 的格式
    for source_run, target_run in zip(source_paragraph.runs, target_paragraph.runs):
        target_run.bold = source_run.bold
        target_run.italic = source_run.italic
        target_run.underline = source_run.underline
        target_run.font.name = source_run.font.name
        target_run.font.size = source_run.font.size
        target_run.font.color.rgb = source_run.font.color.rgb

class Handout:
    '''
    讲义
    '''

    def generate_file(self,topics:[],class_info):
        for row_idx,row in enumerate(table.rows):
            if row_idx==0:
                row.cells[1].paragraphs[0].add_run('001')
                row.cells[3].paragraphs[0].add_run('高一')
                row.cells[4].paragraphs[0].add_run('2')
            if row_idx==1:
                row.cells[1].paragraphs[0].add_run('杀杀杀')
                row.cells[3].paragraphs[0].add_run('python')
                row.cells[4].paragraphs[0].add_run('hfbeh')
            if row_idx==2:
                row.cells[1].paragraphs[0].add_run('三角形')
                row.cells[3].paragraphs[0].add_run('10：00-12：00')

            if row_idx==5:
                cell=row.cells[0]
                paras=cell.paragraphs
                for p in paras:
                    format[p.text]=p
                cell.text=''
                new_p=cell.add_paragraph('内容回顾')
                copy_paragraph_format(format['内容回顾'],new_p)

                new_p = cell.add_paragraph('知识精讲')
                copy_paragraph_format(format['知识精讲'],new_p)

                new_p = cell.add_paragraph('【例题精讲】')
                copy_paragraph_format(format['【例题精讲】'], new_p)
                cell.add_paragraph('中按你的')
                new_p = cell.add_paragraph('【课堂练习】')
                copy_paragraph_format(format['【课堂练习】'],new_p)

                new_p = cell.add_paragraph('预习内容')
                copy_paragraph_format(format['预习内容'],new_p)

    # for col_index,cell in enumerate(row.cells):

Handout().generate_file([],[])
doc.save('temp.docx')

